package com.lagou.rpc.consumer.controller;

import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.consumer.proxy.RpcClientProxy;
import com.lagou.rpc.pojo.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/user")
public class UserController {

    private static IUserService userService = (IUserService) RpcClientProxy.createProxy(IUserService.class);

    @GetMapping("list")
    public ResponseEntity getUsers(){
        List<User> usersList = userService.getUsers();
        return new ResponseEntity(usersList, HttpStatus.OK);
    }

    @GetMapping("{id}")
    public ResponseEntity getUser(@PathVariable Integer id){
        User user = userService.getById(id);
        return new ResponseEntity(user,HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity saveUser(@RequestBody User user){
        userService.saveUser(user);
        return new ResponseEntity(HttpStatus.OK);
    }

}
